/**
 * 连接池
 */

let {db:dbConfig, db} = require('../config/index');
const mysql = require('mysql');

const pool = mysql.createPool({
    host: dbConfig.host,
    user: dbConfig.user,
    password: dbConfig.password,
    database: dbConfig.database,
    charset: dbConfig.charset,
    acquireTimeout: 15000, // 连接超时时间
    connectionLimit: 100, //最大连接数
    waitForConnections: true, // 超过最大连接时排队
    queueLimit: 0 // 排队最大数量(0 代表不做限制)
});

module.exports = {
    query: function (sql, params) {
        return new Promise((resolve, reject) => {
            pool.getConnection((err, conn) => {
                if (err) {
                    return reject(err);
                }
                conn.query(sql, params, (errs, result) => {
                    if (errs) {
                        return reject(errs);
                    }
                    return resolve(result);
                })
            })
        })
    }
}